package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        log.info("程序走到这里了吗");
        //1:将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2:根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //给wrapper赋予条件

        wrapper.eq(Employee::getUsername ,employee.getUsername());
        //用mp中自带的getone方法  将wrpper条件放入
        Employee emp = employeeService.getOne(wrapper);

        //3:开始做判断,没有查询到  返回登陆失败的结果
        if (emp==null){
            return R.error("登陆失败,账号或密码错误");
        }
        //4:判断密码,如果密码错误, 返回失败结果
        if (!emp.getPassword().equals(password)){
            return R.error("登陆失败,账号或密码错误");
        }
        //5:判断员工状态, 如果是已禁用状态,返回员工已禁用状态;
        if (emp.getStatus()==0){
            return R.error("账号已被禁用");
        }
        //6:登录成功,将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);

    }
    @GetMapping("/select")
    public List<Employee> selectid(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        Employee id = employeeService.getById(1);
        System.out.println("高铂汶查到的id"+id);
        return null;
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("推出成功");
    }
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        // 1:  打印请求数据
        log.info("新增数据",employee);
        // 2:  设置初始密码 进行MD5加密
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);
        // 3:设置创建时间和更新时间
        //新方法  LocalDateTime.now   设置当前时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        // 4: 获取员工Id 设置创建和更新用户的id
//        Long empid = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empid);
//        employee.setUpdateUser(empid);
        //5 完成保存业务
        employeeService.save(employee);
        return R.success("添加成功");

    }

    //分页查询案例
    @GetMapping("/page")
    public R<Page>page(int page,int pageSize,String name){
        // 1: 构造分页 构造器
        Page pageInfo = new Page(page, pageSize);
        // 2: 构造条件 构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        // 3: 添加过滤条件
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName,name);
        // 4: 添加排序条件
       // wrapper.orderByAsc(Employee::getId);
        // 5: 执行查询
        employeeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //打印日志   修改内容
        log.info(employee.toString());
        //获取域中数据 , 获取登陆人的id
//        Long employee1 = (Long) request.getSession().getAttribute("employee");
//        //设置更新人  为从狱中拿到的登陆人   修改人 是为管理员
//        employee.setUpdateUser(employee1);
//        //设置更新时间  从新localdatetime.now
//        employee.setUpdateTime(LocalDateTime.now());
        //  根据id修改,
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");

    }
    @GetMapping("/{id}")  //这里的操作和下一行的@PathVariable注解一起出现 ,此为Rest风格  下面接收id 接受的是上面路径上传来的id
    public R<Employee> getById(@PathVariable Long id){
        log.info("这里玩的是查询员工信息");
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }


}
